package com.ld.security.common.mybatis.wrapper;

import com.ld.security.common.core.base.po.OrderByPo;
import com.ld.security.common.core.base.po.QueryPo;
import com.ld.security.common.core.base.po.SuperPo;
import com.ld.security.common.core.util.ReflectionUtil;
import com.ld.security.common.core.util.TemplateUtil;
import com.ld.security.common.mybatis.core.wrapper.EntityWrapper;
import com.ld.security.common.mybatis.core.wrapper.WrapperFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.stereotype.Component;


public class SimpWrapperFactory implements WrapperFactory {


    @Override
    public <V> EntityWrapper<V> getWrapper(SuperPo po) {

        EntityWrapper ew =  new EntityWrapper<>();
        SuperPo q = po;
        if(po instanceof QueryPo){
            QueryPo<SuperPo> qo = (QueryPo) po;
            q =   qo.getQuery();
        }
        if(q != null){
            ew.initWhere(q);
        }

        return ew;
    }


    protected void orderBy(EntityWrapper ew,SuperPo po){
        if (po instanceof OrderByPo){
            OrderByPo orderByPo =(OrderByPo) po;
            if(!ReflectionUtil.isNull(orderByPo.getOrderBy())){
                String columnName = TemplateUtil.javaToColumn(orderByPo.getOrderBy());
                if(orderByPo.isAsc()){
                    ew.orderByAsc(columnName);
                }else{
                    ew.orderByDesc(columnName);
                }
            }

        }
    }




}
